package io;

import java.io.File;
import java.util.Scanner;
//扫描指定目录，并找到名称中包含指定字符的所有普通文件（不包含目录），并且后续询问用户是否要删除该文件
public class IODemo13 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入文件名");
        String fileName = sc.next();
        System.out.println("请输入该目录");
        String rootPath = sc.next();
        File f = new File(rootPath);
        if(!f.isDirectory()){
            System.out.println("给的路径有误");
            return;
        }
        scanDir(f,fileName);
    }

    private static void scanDir(File f, String fileName) {
        File[] files = f.listFiles();
        if(files == null){
            return;
        }
        for(File file : files){
            if(file.isFile()){
                if(file.getName().equals(fileName)){
                    System.out.println("找到了该文件" + file.getAbsolutePath());
                    System.out.println("是否要删除该文件(1.yes,0.no)");
                    Scanner sc = new Scanner(System.in);
                    int n = sc.nextInt();
                    if(n == 1){
                        System.out.println(file.delete());
                    }
                } else if (file.isDirectory()){
                    scanDir(file,fileName);
                } else {
                    ;
                }
            }
        }
    }

}
